package com.zx.onlineeducation.service.cms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zx.onlineeducation.service.cms.entity.BannerDO;
import com.zx.onlineeducation.service.cms.entity.vo.BannerQueryVO;
import com.zx.onlineeducation.service.cms.mapper.BannerMapper;
import com.zx.onlineeducation.service.cms.service.BannerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 * 轮播图 服务实现类
 * </p>
 *
 * @author 郑雪
 * @since 2022-01-23
 */
@Service
public class BannerServiceImpl extends ServiceImpl<BannerMapper, BannerDO> implements BannerService {

    /**
     * 查询轮播图分页数据列表
     *
     * @param current 当前页
     * @param size 每页显示条数
     * @param bannerQueryVO 轮播图查询对象 {@link BannerQueryVO}
     * @return 分页对象 {@link Page} {@link BannerDO}
     * @author 郑雪
     * @since 2022-01-23
     */
    @Override
    public Page<BannerDO> page(Long current, Long size, BannerQueryVO bannerQueryVO) {
        // 1.创建分页对象
        Page<BannerDO> page = new Page<>(current, size);
        // 2.创建条件查询对象
        QueryWrapper<BannerDO> queryWrapper = new QueryWrapper<>();
        String title = bannerQueryVO.getTitle();
        if (!StringUtils.isEmpty(title)) {
            queryWrapper.like("title", title);
        }
        queryWrapper.orderByAsc("sort");
        // 3.执行分页查询
        this.page(page, queryWrapper);
        return page;
    }

    /**
     * 查询所有轮播图
     *
     * @param limit 显示条数
     * @return BannerDO对象的List集合 {@link BannerDO}
     * @author 郑雪
     * @since 2022-01-23
     */
    // @Cacheable 将返回结果进行缓存，以后查询数据，直接从缓存中取，key生成规则：value::key ，例如：banner::selectIndexList
    @Cacheable(value = "banner", key = "'selectIndexList'")
    @Override
    public List<BannerDO> getAll(Integer limit) {
        QueryWrapper<BannerDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("sort");
        if (limit != null && limit > 0 ) {
            queryWrapper.last("limit " + limit);
        }
        return this.list(queryWrapper);
    }

    /**
     * 添加轮播图
     *
     * @param bannerDO 轮播图数据对象 {@link BannerDO}
     * @return 成功true，失败false
     * @author 郑雪
     * @since 2022-01-25
     */
    // @CacheEvict 清空缓存
    @CacheEvict(value = "banner", allEntries = true)
    @Override
    public boolean saveBanner(BannerDO bannerDO) {
        return this.save(bannerDO);
    }

    /**
     * 修改轮播图
     *
     * @param bannerDO 轮播图数据对象 {@link BannerDO}
     * @return 成功true，失败false
     * @author 郑雪
     * @since 2022-01-25
     */
    // @CacheEvict 清空缓存
    @CacheEvict(value = "banner", allEntries = true)
    @Override
    public boolean updateBannerById(BannerDO bannerDO) {
        return this.updateById(bannerDO);
    }

    /**
     * 删除轮播图
     *
     * @param id 轮播图ID
     * @return 成功true，失败false
     * @author 郑雪
     * @since 2022-01-25
     */
    // @CacheEvict 清空缓存
    @CacheEvict(value = "banner", allEntries = true)
    @Override
    public boolean removeBannerById(String id) {
        return this.removeById(id);
    }
}
